// 检查登录状态并更新界面
function checkLoginStatus() {
    const token = localStorage.getItem('token');
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    
    const notLoginEl = document.querySelector('.not-login');
    const loggedInEl = document.querySelector('.logged-in');
    const nicknameEl = document.querySelector('.nickname');
    
    if (token && userInfo) {
        // 已登录状态
        notLoginEl.style.display = 'none';
        loggedInEl.style.display = 'inline-block';
        nicknameEl.textContent = userInfo.nickname || '用户';
    } else {
        // 未登录状态
        notLoginEl.style.display = 'inline-block';
        loggedInEl.style.display = 'none';
    }
}

// 退出登录
async function logout() {
    try {
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
        const res = await axios.get(`/users/logout/${userInfo.id}`);
        
        if (res.data.code === 1) {
            // 清除本地存储
            localStorage.removeItem('token');
            localStorage.removeItem('userInfo');
            // 刷新页面
            location.reload();
        }
    } catch (err) {
        console.error('退出登录失败:', err);
    }
}

// 页面加载时检查登录状态
document.addEventListener('DOMContentLoaded', checkLoginStatus); 